package scene;

import scene.shape.materials.Texture;
import util.Axis;
import util.Color3f;
import util.Vector3f;

public class CubeMap{
	
	private Texture	negative_x,
					negative_y,
					negative_z,
					positive_x,
					positive_y,
					positive_z;
	
	public CubeMap(String negative_x,
			       String negative_y,
			       String negative_z,
			       String positive_x,
			       String positive_y,
			       String positive_z){
		  this.negative_x = new Texture(negative_x, "cubemap_negative_x");
		  this.negative_y = new Texture(negative_y, "cubemap_negative_y");
		  this.negative_z = new Texture(negative_z, "cubemap_negative_z");
		  this.positive_x = new Texture(positive_x, "cubemap_positive_x");
		  this.positive_y = new Texture(positive_y, "cubemap_positive_y");
		  this.positive_z = new Texture(positive_z, "cubemap_positive_z");
	}
	
	public Color3f getColor(Vector3f b){
		float u = 0f,
			  v = 0f;
		Axis a = b.getMaxComponent();
		switch(a){
		case X: 
			if(b.x > 0){
				u = (b.z+b.x)/(2*b.x);
				v = (b.y+b.x)/(2*b.x);
				return this.positive_x.getUV(1-u, v);
			}
			else{
				u = (-b.z+b.x)/(2*b.x);
				v = (-b.y+b.x)/(2*b.x);
				return this.negative_x.getUV(u, v);
			}
		case Y: 
			if(b.y > 0){
				u = (b.x+b.y)/(2*b.y);
				v = (b.z+b.y)/(2*b.y);
				return this.positive_y.getUV(u, 1-v);
			}
			else{
				u = (-b.x+b.y)/(2*b.y);
				v = (-b.z+b.y)/(2*b.y);
				return this.negative_y.getUV(u, v);
			}
		case Z:
			if(b.z > 0){
				u = (b.x+b.z)/(2*b.z);
				v = (b.y+b.z)/(2*b.z);
				return this.positive_z.getUV(u, v);
			}
			else{
				u = (-b.x+b.z)/(2*b.z);
				v = (-b.y+b.z)/(2*b.z);
				return this.negative_z.getUV(1-u, v);
			}
		}
		return null;
	}
}
